package com.elesyser.parser;

import java.util.ArrayList;
import java.util.List;

import com.elesyser.util.ExamInfo;

public class ExamTableParser {
	
	ArrayList<ExamInfo> List = new ArrayList<ExamInfo>();
	
	public ExamTableParser(String source){
		int pos = source.indexOf("id=\"gridMain\"");
		pos = source.indexOf("<tr",pos);
		pos = source.indexOf("<tr",pos  + 1);
		if(!source.substring(pos, pos+4).equals("<tr>")){
			int endpos = source.indexOf("</table>",pos);
			String examString = source.substring(pos, endpos);
			pos = 0;
			endpos = 0;
			while( pos != -1){
				endpos = examString.indexOf("</tr>",pos);
				String tempString = examString.substring(pos,endpos);
				ExamInfo exam = new ExamInfo();
				int temppos = tempString.indexOf("<td");
				temppos = tempString.indexOf(">",temppos) + 1;
				int tempendpos = tempString.indexOf(" ",temppos);
				String name = tempString.substring(temppos,tempendpos);
				exam.setCourseName(name);
				temppos = tempString.indexOf("<td",tempendpos);
				temppos = tempString.indexOf(">",temppos) + 1;
				tempendpos = tempString.indexOf("</td>",temppos);
				temppos = tempString.indexOf("<td",tempendpos);
				temppos = tempString.indexOf(">",temppos) + 1;
				tempendpos = tempString.indexOf("</td>",temppos);
				String time = tempString.substring(temppos,tempendpos);
				exam.setTime(time);
				temppos = tempString.indexOf("<td",tempendpos);
				temppos = tempString.indexOf(">",temppos) + 1;
				tempendpos = tempString.indexOf("</td>",temppos);
				String location = tempString.substring(temppos,tempendpos);
				exam.setLocation(location);
				temppos = tempString.indexOf("<td",tempendpos);
				temppos = tempString.indexOf(">",temppos) + 1;
				tempendpos = tempString.indexOf("</td>",temppos);
				String teacher = tempString.substring(temppos,tempendpos);
				exam.setTeacher(teacher);
				List.add(exam);
				pos = examString.indexOf("<tr",endpos);
			}
		}
		else
			List=null;
	}
	
	public List<ExamInfo> getExams(){
		return List;
	}
	
}
